package me.shep.wishlist.client;

import me.shep.wishlist.client.RPC.InventoryServiceAsync;

import com.google.gwt.user.client.Cookies;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;

public class ItemInfoWidget extends Composite {
	DesiredItem item;
	Person user;
	Button delete;
	Button purchase;
	Button save;
	FlexTable table = new FlexTable();
	InventoryChangedCallback icc;
	public ItemInfoWidget(DesiredItem d, Person u, InventoryChangedCallback i) {
		item = d;
		user = u;
		icc = i;
		save = new Button("Save", new ClickListener() {
            public void onClick(Widget sender) {
            	//InventoryServiceAsync.impl.deleteItem(item.getId(), Cookies.getCookie("token"), deleteItemCallback);
            }
          });
		
		purchase = new Button("Purchase!", new ClickListener() {
            public void onClick(Widget sender) {
            	InventoryServiceAsync.impl.purchaseItem(item.getId(), Cookies.getCookie("token"), purchaseItemCallback);
            }
          });
		delete = new Button("Delete", new ClickListener() {
            public void onClick(Widget sender) {
            	if(Window.confirm("Are you sure you want to delete " + item.getName() + " from " + item.getWhoWantsIt() + "'s list?"))
            		InventoryServiceAsync.impl.deleteItem(item.getId(), Cookies.getCookie("token"), deleteItemCallback);
            }
          });
		
		
		table.setCellPadding(15);
		refreshTable();
		
		initWidget(table);
	}
	
	public void refreshTable() {
		int i = 0;
		table.setText(i++, 0, "Item Name:");
		//table.setText(i++, 0, "Link:");
		table.setText(i++, 0, "Description:");

		table.setText(i++, 0, "Added By:");
		if(!user.getName().equals(item.getWhoWantsIt())) {
			table.setText(i++, 0, "Already Purchased?");
			table.setText(i++, 0, "Show on their list?");
		}
		i = 0;
		table.setWidget(i++, 1, (item.getUrl() == null  || item.getUrl() == "") ? new Label(item.getName()) : new HTML("<a href='"+item.getUrl()+"' target='_blank_'>"+item.getName()+"</a>"));
		//table.setText(i++, 1, item.getName());
		//table.setWidget(i++, 1, new Hyperlink(item.getUrl(),item.getUrl()));
		table.setText(i++, 1, item.getDescription());
		table.setText(i++, 1, item.getWhoAddedIt().equals(user.getName()) ? "You" : item.getWhoAddedIt());
		if(!user.getName().equals(item.getWhoWantsIt())) {
			table.setText(i++, 1, item.getWhoPurchasedIt() == null ? "No" : "Yes, by " + (item.getWhoPurchasedIt().equals(user.getName()) ? "You" : item.getWhoPurchasedIt()));
			table.setText(i++, 1, item.wanterCanSee ? "Yes" : "No");
			if(item.getWhoPurchasedIt() == null)
				table.setWidget(i, 1, purchase);
		}
		table.setWidget(i, 0, delete);
	}
	//Move this to parent?
	AsyncCallback deleteItemCallback = new AsyncCallback() {
		  public void onFailure(Throwable caught) {
			  Window.alert("There was an error deleting the item!\n\n"+caught.getMessage());

	  }
	
	  public void onSuccess(Object arg0) {
		  Window.alert("Item Deleted Succesfully");
		  icc.deleteItemCallback(item);
		  //delete.setText("Delete");
	  }
	};
	
	AsyncCallback purchaseItemCallback = new AsyncCallback() {
		  public void onFailure(Throwable caught) {
			  Window.alert("There was an error purchasing the item!\n\n"+caught.getMessage());

	  }
	
	  public void onSuccess(Object arg0) {
		  Window.alert("Item Purchased Succesfully");
		  item.setWhoPurchasedIt(user.getName());
		  refreshTable();
		  icc.purchasedItemCallback(item);
	  }
	};
	
}